import java.util.ArrayDeque;

public class LeetCode {
    public int calculate(String s) {
        ArrayDeque<Integer> deque = new ArrayDeque<>();
        char op = '+';
        int i =  0;
       while(i<s.length()) {
            if(s.charAt(i)==' '){
                i++;
            }else if(s.charAt(i)>='0' && s.charAt(i)<='9'){
                int tmp = 0;
                while(i<s.length() && s.charAt(i)>='0' && s.charAt(i)<='9'){
                    tmp = tmp*10+(s.charAt(i)-'0');
                    i++;
                }
                if(op == '+'){
                    deque.push(tmp);
                } else if(op == '-'){
                    deque.push(-tmp);
                } else if(op == '*'){
                    deque.push(deque.pop()*tmp);
                } else{
                    deque.push(deque.pop()/tmp);
                }
            }else{
                op = s.charAt(i);
                i++;
            }
        }
       int ret = 0;
       while(!deque.isEmpty()){
           ret+=deque.pop();
       }
       return ret;
    }
}
